/*
 * Copyright (c) 2024. Shanghai HEADING information Engineering Co., Ltd. All rights reserved.
 */
package com.lovely602.design.study.socket;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author lizhixing
 */
public class TCPServerTest {

  public static void main(String[] args) {

    ServerSocket ss = null;
    Socket s = null;
    try {
      ss = new ServerSocket(8080);
      s = ss.accept();
      OutputStream os = s.getOutputStream();
      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
      String text = "输出：TCP server BufferedWriter";
      bw.write(text);
      bw.flush();
      bw.close();
      os.close();
      System.out.println(text);
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (s != null) {
        try {
          s.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if (ss != null) {
        try {
          ss.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }

  }
}
